<extend name="Base:main" />
<block name="script">
	<link rel="stylesheet" href="__PUBLIC__/seller/css/validform.css" />
</block>
<block name="content">
	<style>
		dt+dd { margin:0px; padding:0px; }
	</style>
    <div class="tpl-content-wrapper">
            <div class="tpl-content-page-title">
               选货
            </div>
            <ol class="am-breadcrumb">
                <li><a href="#" class="am-icon-home">首页</a></li>
                <li><a href="#">商品管理</a></li>
                <li class="am-active">选货</li>
            </ol>
        
            <div class="tpl-portlet-components">
                    <div class="portlet-title">
                      <div class="caption font-green bold">
                        <span class="am-icon-code"> 确认订单信息</span>
                        </div>
                    </div>
                    <div class="tpl-block ">
            <form action="" name='myfromSubmit' method="post"  >
                   <input type="hidden" name = "distribution_id" id="distribution_id" value="{$info['id']}">
                     <input type="hidden"  id="address_id" name="address_id" value="{$outh['address_id']}">
                        
                        <div class="am-g tpl-amazeui-form">
                            <div class="am-u-sm-12 am-u-md-12">
									<fieldset class="fieldset_list">
										<legend>收货地址</legend>
										<div class="supply_info">
											<dl>
												<dt>收货人：</dt>
												<dd>{$address.truename}</dd>
											</dl>
											<dl>
												<dt>联系电话：</dt>
												<dd>{$address.phone}</dd>
											</dl>
											<dl style="width:100%;">
												<dt>收货地址：</dt>
												<dd>{$address.city} {$address.address}</dd>
											</dl>
										</div>
									</fieldset>
									<fieldset class="fieldset_list">
										<legend>发布物流单：
											<input id="r1" type="radio" value="1" name="chose" >是</input>
											<input id="r2" type="radio" value="0" name="chose" checked>否</input>
										</legend>
										<div>
											<input id="rr1" type="radio" value="1" name="chose1" >在线发布</input>
											<input id="rr2" type="radio" value="2" name="chose1" checked>线下联系</input>
										</div>
									</fieldset>
									<?php if($info['distribution_stock']>0):?>
									<fieldset class="fieldset_list">
										<legend>锁货产品信息</legend>
										<table class="am-table am-table-striped am-table-hover table-main">
											 <tr>
												<th width="80px">商品名称</th>
												<th width="80px">供货商</th>
												<th width="150">价格</th>
												<th width="150px">数量</th>
												<th width="150px">物流方式</th>
												<th width="150px">物流费用</th>
												<th width="150px">总价</th>
											</tr>
											<volist name="distribution" id="v1">
											 <tr id="tr_list">
												<td>{$info['name']}</td>
												<td>锁货<input type="hidden" name="distribution[{$v1.id}][distribution_product_id]" value="{$v1.id}"></td>
												<td style="color: red;"><input type="hidden" id="price"  value="{$v1['cost_price']}">￥{$v1['cost_price']}</td>
												<td><button type="button" style="width:25px;height: 25px;" onclick="j_btn(this,'',1)">-</button><input type="text" onkeyup="if( isNaN( this.value ) || this.value > <?php echo $v1['stock_num'];?>) { this.value = 1; amount();} else {amount(); }" style="border: 1px solid  !important; width: 30px;text-align:center;-webkit-appearance: none; " id="number" name="distribution[{$v1.id}][number]" value="<?php if($info['d_max_key']>$key && $info['d_max_key']>0):?>{$v1['stock_num']}<?php elseif($info['d_max_key']==$key):?>{$info['d_max_num']}<?php else:?>0<?php endif;?>"><button type="button" max-num="{$v1['stock_num']}"   onclick="a_btn(this,'',1);" style="width:25px;height: 25px;">+</button>库存为{$v1['stock_num']}</td>
												<td><select name="distribution[{$v1.id}][dispatching_id]" onchange="dispatching()">
														<?php if(count( $v1['dispatching'])==0):?>
														<option value="0">无物流地址不能购买</option>
														<?php endif;?>
														   <?php foreach( $v1['dispatching'] as $k => $v ) { ?>
															 <option value="{$v['id']}" first-num="{$v.first_num}" first-price="{$v.first_price}" add-first-num="{$v.add_first_num}" add-first-price="{$v.add_first_price}">{$v.name}</option>
														 <?php } ?>
														 <option value="0">买家自己提供物流</option>
												 </select><!--<span id="explain">{$val['dispatching'][0]['first_num']}件{$val['dispatching'][0]['first_price']}元，递增{$val['dispatching'][0]['add_first_num']}件{$val['dispatching'][0]['add_first_price']}元</span>--></td>
												<td style="color: red;" >￥<span id="fee"></span></td>                   
											   <td style="color: red;">￥<span id="amount"><?php if($info['d_max_key']>$key):?><?php echo $v1['stock_num']*$v1['cost_price'];?><?php elseif($info['d_max_key']==$key && $key>0):?><?php echo $info['d_max_num']*$v1['cost_price'];?><?php else:?>0<?php endif;?></span></td>
											  
											</tr>
											</volist>
										</table> 
									</fieldset>
									<?php endif;?>
									<fieldset class="fieldset_list">
										<legend>共享产品信息</legend>
										<table class="am-table am-table-striped am-table-hover table-main">
											 <tr>
												<th width="80px">商品名称</th>
												<th width="80px">供货商</th>
												<th width="150">价格</th>
												<th width="150px">数量</th>
												<th width="150px">物流方式</th>
												<th width="150px">物流费用</th>
												<th width="150px">总价</th>
											</tr>
										 
											<volist name="supplys" id="val" >
											<tr id="tr_list">
												<td>{$info['name']}</td>
												<td>货源{$key+1}<input type="hidden" name="supply[{$val.id}][supply_product_id]"></td>
												<td style="color: red;">
													<input type="hidden" id="price" class="price_{$val['id']}"  value="{$val['acting_price']}" />
													￥<span id="price_{$val['id']}">{$val['acting_price']}</span>
												</td>
												<td><button type="button" style="width:25px;height: 25px;" onclick="j_btn(this,<?php echo $val['id'];?>)">-</button><input type="text"  onkeyup="if( isNaN( this.value ) || this.value > <?php echo $val['stock_num'];?>) { this.value = 1; anumber(this.value,<?php echo $val['id'];?>);} else {anumber(this.value,<?php echo $val['id'];?>); }" style="border: 1px solid  !important; width: 30px;text-align:center;-webkit-appearance: none; " id="number" name="supply[{$val.id}][number]" value="<?php if($info['max_key']>$key && $info['max_key']>0):?>{$val['stock_num']}<?php elseif($info['max_key']==$key):?>{$info['max_num']}<?php else:?>0<?php endif;?>"><button type="button" max-num="{$val['stock_num']}"   onclick="a_btn(this,<?php echo $val['id'];?>);" style="width:25px;height: 25px;">+</button>库存为{$val['stock_num']}</td>
												<td><select name="supply[{$val.id}][dispatching_id]" onchange="dispatching(this)">
														<?php if(count( $val['dispatching'])==0):?>
														<option value="0">无物流地址不能购买</option>
														<?php endif;?>
														<?php foreach( $val['dispatching'] as $k => $v ) { ?>
															 <option value="{$v['id']}" first-num="{$v.first_num}" first-price="{$v.first_price}" add-first-num="{$v.add_first_num}" add-first-price="{$v.add_first_price}">{$v.name}</option>
														 <?php } ?>   

												 </select><!--<span id="explain">{$val['dispatching'][0]['first_num']}件{$val['dispatching'][0]['first_price']}元，递增{$val['dispatching'][0]['add_first_num']}件{$val['dispatching'][0]['add_first_price']}元</span>--></td>
												<td style="color: red;" >￥<span id="fee"></span></td>  
												<td style="color: red;">￥<span id="amount"><?php if($info['max_key']>$key && $info['max_key']>0):?><?php echo $val['stock_num']*$val['acting_price'];?><?php elseif($info['max_key']==$key ):?><?php echo $info['max_num']*$val['acting_price'];?><?php else:?>0<?php endif;?></span></td>
											  
											</tr>
											</volist>
										</table> 
									</fieldset>
                            </div>
                           
                        </div>
                                <div class="hr hr8 hr-double hr-dotted"></div>
                                <div class="tpl-portlet-input tpl-fz-ml">
                                    <div class="portlet-input  input-inline">
                                            <span class="invoice-info-label">订单总价:</span>
                                            <span id="total" style="color: #dd5a43 !important;">￥{$info['cost_price']} 元</span> 
                                     </div>
                                </div>
                       <div class="am-form-group">
                                <div class="am-u-sm-12 am-u-sm-push-11">
                                        <button type="submit" class="am-btn am-btn-primary">提交订单</button>
                                      
                                </div>
                        </div>
                    </div>
                     </form>
            </div>
    </div>    
    <script>
        function anumber(num,supply_product_id){
            var url = getUrl('Goods/price',{'num':num,'supply_product_id':supply_product_id});
                $.get(url,function(data){
                    if(data.errno == 1){
                       $('#price_'+supply_product_id).html(data.price);
                       $('.price_'+supply_product_id).val(data.price);
                          amount();
                    }else{
                        alert(data.msg);
                    }
                },'json')
        }
		function dispatching(obj){
			var first_num =  parseInt($(obj).find('option:selected').attr('first-num'));
			var first_price =  $(obj).find('option:selected').attr('first-price');
			var add_first_num =  parseInt($(obj).find('option:selected').attr('add-first-num'));
			var add_first_price =  $(obj).find('option:selected').attr('add-first-price');
			$(obj).next("#explain").text(first_num+'件'+first_price+'元，递增'+add_first_num+'件'+add_first_price+'元');
			amount();
		}
        
		function j_btn(obj,supply_product_id,type){
			var num = $(obj).next().val();
			--num;
			if(num>0){
				$(obj).next().val(num)
				if(type ==1){
					amount();
				}else{
					anumber(num,supply_product_id);
				}
			}
		}
        function a_btn(obj,supply_product_id,type){
            var stock = $(obj).attr("max-num");
            var num = $(obj).prev().val();
            ++num;
            if(num>0 && num<= stock){
                $(obj).prev().val(num);
                 if(type ==1){
                   amount();
               }else{
                   anumber(num,supply_product_id);
               }
            }
        }
        function amount(){
            var total = 0;
            $("tr#tr_list").each(function(k){
               var price = $(this).find("#price").val();
               var numb = $(this).find("#number").val();
               if(price>0 && numb>0){
                    var fee = 0;
                     if($(this).find('option:selected').val() >0){
                          var first_num =  parseInt($(this).find('option:selected').attr('first-num'));
                          var first_price =  $(this).find('option:selected').attr('first-price');
                          var add_first_num =  parseInt($(this).find('option:selected').attr('add-first-num'));
                          var add_first_price =  $(this).find('option:selected').attr('add-first-price');
                          if(numb>first_num && first_num>0){
                              fee = Math.round(first_price*100)/100 + Math.round(Math.ceil((numb-first_num)/add_first_num)*add_first_price*100)/100;
                          }else{
                             fee =  Math.round(first_price*100)/100;
                          }
                         $(this).find("#fee").text(fee.toFixed(2));
                     } else {
						fee = 0;
						$(this).find("#fee").text("0.00");
					 }
                     total = Math.round(price*numb*100)/100+Math.round(total*100)/100 + Math.round(fee*100)/100;
                     var amount = Math.round(price*numb*100)/100+Math.round(fee*100)/100
                     $(this).find("#amount").text(amount.toFixed(2));
               }
             
            });
            $("#total").text('￥'+total.toFixed(2)+'元');
        }
		$(function(){
			amount();
		});
    </script>
</block>    